package com.ruoyi.system.service.impl.smy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.BanquetConstants;
import com.ruoyi.common.constant.OrderType;
import com.ruoyi.common.core.domain.smy.MallGoods;
import com.ruoyi.common.core.domain.smy.MallGoodsImage;
import com.ruoyi.common.core.domain.smy.MallGoodsParams;
import com.ruoyi.common.core.domain.smy.MallGoodsSku;
import com.ruoyi.common.core.domain.smy.SecurityWebUser;
import com.ruoyi.common.dto.OptDto;
import com.ruoyi.system.mapper.smy.MallGoodsMapper;
import com.ruoyi.system.service.smy.IMallGoodsImageService;
import com.ruoyi.system.service.smy.IMallGoodsParamsService;
import com.ruoyi.system.service.smy.IMallGoodsService;
import com.ruoyi.system.service.smy.IMallGoodsSkuService;
import com.ruoyi.system.service.smy.ISecurityWebUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class MallGoodsServiceImpl implements IMallGoodsService {
	@Resource
	private MallGoodsMapper mallGoodsMapper;

	@Resource
	private IMallGoodsImageService goodsImageService;

	@Resource
	private IMallGoodsParamsService goodsParamsService;

	@Resource
	private ISecurityWebUserService securityWebUserService;

	@Resource
	private IMallGoodsSkuService skuService;

	@Override
	public MallGoods selectMallGoodsById(Long id) {
		return mallGoodsMapper.selectMallGoodsById(id);
	}

	@Override
	public List<MallGoods> selectMallGoodsList(MallGoods mallGoods) {
		return mallGoodsMapper.selectMallGoodsList(mallGoods);
	}

	@Override
	public int insertMallGoods(MallGoods mallGoods) {
		return mallGoodsMapper.insertMallGoods(mallGoods);
	}

	@Override
	public int updateMallGoods(MallGoods mallGoods) {
		List<MallGoodsImage> goodsImageList = mallGoods.getGoodsImageList();
		goodsImageService.saveOrUpdateGoodsImage(mallGoods,goodsImageList);



		//设置包工包料与包工不包料属性
		if(NumberUtil.compare(mallGoods.getNomaterialprice(),0)>0){
			mallGoods.setNomaterial(BanquetConstants.YES);
		}else{
			mallGoods.setNomaterial(BanquetConstants.NO);
		}
		if(NumberUtil.compare(mallGoods.getWithmaterialprice(),0)>0){
			mallGoods.setWithmaterial(BanquetConstants.YES);
		}else{
			mallGoods.setWithmaterial(BanquetConstants.NO);
		}
		return mallGoodsMapper.updateMallGoods(mallGoods);
	}

	@Override
	public int deleteMallGoodsByIds(Long[] ids) {
		List<Long> longIdList = CollectionUtil.newArrayList(ids);
		List<MallGoods> mallGoodsList = mallGoodsMapper.selectMallGoodsListByIds(longIdList);
		List<MallGoods> updateList = CollectionUtil.newArrayList();
		for(MallGoods mallGoods : mallGoodsList){
			if(NumberUtil.compare(mallGoods.getStatus(),BanquetConstants.APPROVE)==0
					&& !StrUtil.equals(mallGoods.getSellstatus(),BanquetConstants.soldOut)){
				throw new ValidateException(mallGoods.getName()+"状态不允许删除");

			}
			mallGoods.setStatus(BanquetConstants.DELETE);
			updateList.add(mallGoods);
		}
		if(CollectionUtil.isNotEmpty(updateList)){
			updateList.forEach(item->mallGoodsMapper.updateMallGoods(item));
		}
		return updateList.size();
	}

	@Override
	public int deleteMallGoodsById(Long id) {
		return mallGoodsMapper.deleteMallGoodsById(id);
	}

	@Override
	public List<MallGoods> selectMallGoodsPage(MallGoods mallGoods) {
		return mallGoodsMapper.selectMallGoodsPage(mallGoods);
	}

	@Override
	public void auditGoods(OptDto optDto) {
		String auditType = optDto.getOptType();
		List<Long> longIdList = optDto.getLongIdList();
		List<MallGoods> mallGoodsList = mallGoodsMapper.selectMallGoodsListByIds(longIdList);
		List<MallGoods> updateList = CollectionUtil.newArrayList();
		for (MallGoods mallGoods : mallGoodsList) {
			if (NumberUtil.compare(mallGoods.getStatus(), BanquetConstants.CREATE) == 0) {
				if (StrUtil.equals(BanquetConstants.YES, auditType)) {
					mallGoods.setStatus(BanquetConstants.APPROVE);
					mallGoods.setSellstatus(BanquetConstants.onSale);
				}
				if (StrUtil.equals(BanquetConstants.NO, auditType)) {
					mallGoods.setStatus(BanquetConstants.AUDIT_FAIL);
				}
				updateList.add(mallGoods);
			}
		}
		if(CollectionUtil.isNotEmpty(updateList)){
			updateList.forEach(item->mallGoodsMapper.updateMallGoods(item));
		}
	}

	@Override
	public void changeSellStatus(OptDto optDto) {
		List<Long> longIdList = optDto.getLongIdList();
		List<MallGoods> mallGoodsList = mallGoodsMapper.selectMallGoodsListByIds(longIdList);
		List<MallGoods> updateList = CollectionUtil.newArrayList();
		for (MallGoods mallGoods : mallGoodsList) {
			if(StrUtil.equals(BanquetConstants.YES,optDto.getOptType())
					&& !StrUtil.equals(BanquetConstants.onSale,mallGoods.getSellstatus())){
				mallGoods.setSellstatus(BanquetConstants.onSale);
				updateList.add(mallGoods);
			}
			if(StrUtil.equals(BanquetConstants.NO,optDto.getOptType())
					&& !StrUtil.equals(BanquetConstants.soldOut,mallGoods.getSellstatus())){
				mallGoods.setSellstatus(BanquetConstants.soldOut);
				updateList.add(mallGoods);
			}
		}
		if(CollectionUtil.isNotEmpty(updateList)){
			updateList.forEach(item->mallGoodsMapper.updateMallGoods(item));
		}
	}

	@Override
	public MallGoods getGoodsDetail(MallGoods mallGoods) {
		MallGoods dbGoods = mallGoodsMapper.selectMallGoodsById(mallGoods.getId());
		if(Objects.nonNull(dbGoods)){
			MallGoodsImage goodsImage = new MallGoodsImage();
			goodsImage.setGoodsid(dbGoods.getId());
			List<MallGoodsImage> goodsImageList = goodsImageService.selectMallGoodsImageList(goodsImage);
			dbGoods.setGoodsImageList(goodsImageList);

			MallGoodsParams goodsParams = new MallGoodsParams();
			goodsParams.setGoodsid(dbGoods.getId());
			List<MallGoodsParams> goodsParamsList = goodsParamsService.selectMallGoodsParamsList(goodsParams);
			dbGoods.setGoodsParamsList(goodsParamsList);
		}
		return dbGoods;
	}

	@Override
	public void saveNormalGoods(MallGoods mallGoods) {
		List<MallGoodsSku> goodsSkuList = mallGoods.getGoodsSkuList();
		List<MallGoodsParams> goodsParamsList = mallGoods.getGoodsParamsList();
		List<MallGoodsImage> goodsImageList = mallGoods.getGoodsImageList();
		//设置状态为新建
		mallGoods.setStatus(1L);
		Long totalStore = 0L;
		for(MallGoodsSku sku : goodsSkuList){
			totalStore += sku.getStore();
		}
		mallGoods.setStore(totalStore);
		mallGoods.setRecommend(0);
		mallGoods.setSalesvolume(0L);
		mallGoods.setPostage(0L);
		mallGoods.setPostagetype(0L);
		mallGoods.setGram(0L);
		mallGoods.setBargainprice(0L);
		mallGoods.setBargainstore(0L);
		mallGoods.setBargainstatus(0L);

		mallGoods.setMarketingtype(0L);
		mallGoods.setBargainsalesvolume(0L);
		mallGoods.setCreatetime(new Date());
		mallGoods.setStatus(2L);
		mallGoods.setSellstatus(OrderType.SOLD_OUT);
		mallGoodsMapper.insertMallGoods(mallGoods);

		/**
		 * 创建SKU
		 */
		if(CollectionUtil.isNotEmpty(goodsSkuList)){
			buildGoodsSku(mallGoods,goodsSkuList);
		}
		/**
		 * 创建保存参数列表
		 */
		if(CollectionUtil.isNotEmpty(goodsParamsList)){
			buildGoodsParam(mallGoods,goodsParamsList);
		}
		/**
		 * 创建保存商品图片
		 */
		if(CollectionUtil.isNotEmpty(goodsImageList)){
			buildGoodsImage(mallGoods,goodsImageList);
		}
	}

	private void buildGoodsImage(MallGoods mallGoods, List<MallGoodsImage> goodsImageList) {
		for(MallGoodsImage image: goodsImageList){
			image.setCreatetime(new Date());
			image.setGoodsid(mallGoods.getId());
			image.setMerchantId(mallGoods.getMerchantId());
			goodsImageService.insertMallGoodsImage(image);
		}
	}

	private void buildGoodsParam(MallGoods mallGoods, List<MallGoodsParams> goodsParamsList) {
		for(MallGoodsParams params : goodsParamsList){
			params.setGoodsid(mallGoods.getId());
			params.setMerchantId(mallGoods.getMerchantId());
			goodsParamsService.insertMallGoodsParams(params);
		}
	}

	private void buildGoodsSku(MallGoods mallGoods, List<MallGoodsSku> goodsSkuList) {
		List<MallGoodsSku> saveList = CollectionUtil.newArrayList();
		for(MallGoodsSku sku : goodsSkuList){
			sku.setGoodsid(mallGoods.getId());
			sku.setMerchantId(mallGoods.getMerchantId());
			String[] attributeList = sku.getSkuno().split("-");
			for(int i=0;i<attributeList.length;i++){
				Long attributeId = Long.valueOf(attributeList[i]);
				if(i==0){
					sku.setAttribute1Id(attributeId);
				}
				if(i==1){
					sku.setAttribute2Id(attributeId);
				}
				if(i==2){
					sku.setAttribute3Id(attributeId);
				}
				if(i==3){
					sku.setAttribute4Id(attributeId);
				}
				if(i==4){
					sku.setAttribute5Id(attributeId);
				}
			}
			saveList.add(sku);
		}
		if(CollectionUtil.isNotEmpty(saveList)){
			saveList.forEach(item->skuService.insertMallGoodsSku(item));
		}
	}
}
